﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System.Collections.Generic;
    using System.Windows.Controls;

    public class Scene
    {
        public List<VectorObject> vectorObjects = new List<VectorObject>();

        public void Add(VectorObject vectorObject)
        {
            this.vectorObjects.Add(vectorObject);
        }

        public void Clear()
        {
            this.vectorObjects.Clear();
        }

        public void Draw(Projector projector, Canvas canvas, IShader shader, Vector rotationCenter)
        {
            List<VectorObject> vectorObjectsInView = new List<VectorObject>();

            for (int i = 0; i < this.vectorObjects.Count; i++)
            {
                double maxZ = this.vectorObjects[i].GetMaxZAfterRotation(projector, rotationCenter);

                if (maxZ < projector.ViewPosition.Z + projector.ViewFarDistance && maxZ >= projector.ViewPosition.Z + projector.ViewNearDistance)
                {
                    this.vectorObjects[i].CachedMaxZAfterRotation = maxZ;
                    vectorObjectsInView.Add(this.vectorObjects[i]);
                }
            }

            vectorObjectsInView.Sort();

            for (int i = 0; i < vectorObjectsInView.Count; i++)
            {
                vectorObjectsInView[i].Draw(projector, canvas, shader, rotationCenter);
            }
        }
    }
}